package com.gliese.util.file;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.URL;

public class FileWriterHelper {
	public static Writer wirteToClassPath(String str) {
		URL url = FileWriterHelper.class.getResource(str);
		BufferedWriter bw = null;
		try {
			bw = new BufferedWriter(new OutputStreamWriter(
					new FileOutputStream(new File(url.getFile()))));
		} catch (FileNotFoundException e) {
			throw new RuntimeException(str + " File Not Found Exception");
		}

		return bw;
	}

	/**
	 * write file by <code>str</code> ,such as sample/hello ,will create
	 * ~/.sample/hello file .
	 * 
	 * @param str
	 * @return
	 */
	public static Writer writeToUserHome(String str) {
		BufferedWriter bw = null;
		String absoluteUrl = System.getProperty("user.home")
				+ System.getProperty("file.separator") + "." + str;
		try {
			File file = new File(absoluteUrl);
			if (!file.exists())
				file.createNewFile();
			bw = new BufferedWriter(new OutputStreamWriter(
					new FileOutputStream(file)));
		} catch (Exception e) {
			throw new RuntimeException(str + " File created,errors occur!");
		}
		return bw;
	}

	public OutputStream writeToResourceByUrl(String str) {
		throw new RuntimeException(
				"FIleWriter.writeToResorcesByUrl is not implemented !");
		// !FIXME
	}
}
